import requests
import execjs
import re

cookies = {
    'qgqp_b_id': 'ed395e97245ace7ecb9f0e29f091862d',
    'websitepoptg_api_time': '1623891599924',
    'st_si': '32730208548419',
    'st_asi': 'delete',
    'HAList': 'ty-0-300059-%u4E1C%u65B9%u8D22%u5BCC',
    'st_pvi': '60017943722354',
    'st_sp': '2024-08-17%2020%3A09%3A59',
    'st_inirUrl': 'https%3A%2F%2Fcn.bing.com%2F',
    'st_sn': '5',
    'st_psi': '20240817201259254-117001314791-5945157549',
}

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Pragma': 'no-cache',
    'Referer': 'https://guba.eastmoney.com/',
    'Sec-Fetch-Dest': 'script',
    'Sec-Fetch-Mode': 'no-cors',
    'Sec-Fetch-Site': 'same-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0',
    'sec-ch-ua': '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

params = {
    'type': '0',
    'sort': '0',
    'page': '1',
    'v': '2024_8_18_1_0',
}

response = requests.get('https://gbcdn.dfcfw.com/rank/popularityList.js', params=params, headers=headers).text
response = re.findall("var popularityList='(.*?)'", response)[0]
print(response)

jscode = open("./main.js", "r", encoding='utf-8').read()
scriptData = execjs.compile(jscode).call("decode", response)
code = []
for it in scriptData:
    code.append(it["code"])

secids = ','.join(code)

secids = execjs.compile(jscode).call("getHQSecIdByMutiCode", secids)

print(secids)

response = requests.get(
    'https://push2.eastmoney.com/api/qt/ulist.np/get?fltt=2&np=3&ut=a79f54e3d4c8d44e494efb8f748db291&invt=2&secids=' + secids + '&fields=f1,f2,f3,f4,f12,f13,f14,f152,f15,f16',
    cookies=cookies,
    headers=headers,
)

for it in response.json()["data"]["diff"]:
    print(it)
